﻿var jcrop_api;


var update = function (coords) {
    if (parseInt(coords.w) <= 0 || parseInt(coords.h) <= 0) return;

    x = coords.x;
    y = coords.y;
    x2 = coords.x2;
    y2 = coords.y2;
    w = coords.w;
    h = coords.h;

    innerHeight = previewMaxHeight;
    innerWidth = previewMaxWidth;


    var cropRatio = coords.w / coords.h;


    var path = image.attr('src');



    var innerWidth = cropRatio >= (previewMaxWidth / previewMaxHeight) ? previewMaxWidth : previewMaxHeight * cropRatio;
    var innerHeight = cropRatio < (previewMaxWidth / previewMaxHeight) ? previewMaxHeight : previewMaxWidth / cropRatio;


    innerPreview.css({
        width: innerWidth + 'px',
        height: innerHeight + 'px',
        marginTop: (previewMaxHeight - innerHeight) / 2 + 'px',
        marginLeft: (previewMaxWidth - innerWidth) / 2 + 'px',
        overflow: 'hidden'
    });
    divPreview.css({

    });


    var scalex = innerWidth / coords.w;
    var scaley = innerHeight / coords.h;

    $('#innerPreview').find('img').css({
        width: (scalex * image.width()) + 'px',
        height: (scaley * image.height()) + 'px',
        marginLeft: '-' + Math.round(scalex * coords.x) + 'px',
        marginTop: '-' + Math.round(scaley * coords.y) + 'px'
    });

    //query = '?';

    query = 'crop=(' + coords.x + ',' + coords.y + ',' + coords.x2 + ',' + coords.y2 + ')&width=131&height=131';

    //$('#result').attr('href', path + query);

    $("#crop").val(query);

}